const Core = require('@alicloud/pop-core');
const jwt = require("jsonwebtoken");
module.exports = {
    verifyToken(req, res) {
        // 获取token
        const token = req.headers.token || req.body.token || req.query.token;
        return new Promise((resolve, reject) => {
            if (token) {
                // 验证token
                jwt.verify(token, 'cmhtokenkey', (err, decoded) => {
                    if (err) {
                        res.send({
                            code: '10010',
                            message: 'token验证失败' + err
                        });
                    } else {
                        req.decoded = decoded;
                        resolve();
                    }
                })
            } else {
                res.send({
                    code: '10010',
                    message: 'token验证失败'
                });
            }
        })
    },
    sendCode(PhoneNumbers, TemplateParam) {

        let client = new Core({
            accessKeyId: 'LTAI4G55k6SsmVbjXeqyqKbc',
            accessKeySecret: 'F2ch4np5pZeV6MNdagi8wHVwTktBGk',
            endpoint: 'https://dysmsapi.aliyuncs.com',
            apiVersion: '2017-05-25'
        });

        let params = {
            "RegionId": "cn-hangzhou",
            "PhoneNumbers": PhoneNumbers,
            "SignName": "盟商",
            "TemplateCode": "SMS_199202633",
            "TemplateParam": "{code:" + TemplateParam + "}"
        }

        let requestOption = {
            method: 'POST'
        };

        return new Promise((resolve, reject) => {
            client.request('SendSms', params, requestOption).then((result) => {
                resolve(JSON.stringify(result));
            }, (ex) => {
                reject(ex);
            });
        })
    }
}
